package framework.module.jep.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.singularsys.jep.Jep;
import com.singularsys.jep.JepException;
import com.singularsys.jep.configurableparser.StandardConfigurableParser;
import com.singularsys.jep.misc.javaops.JavaOperatorTable;
import com.singularsys.jep.parser.Node;
import framework.common.ResponseVo;
import frameworkExt.util.controller.BaseControllerExt;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 参考博客：https://blog.csdn.net/qq_34966814/article/details/79662443 <br/>
 *           https://blog.csdn.net/CHS007chs/article/details/51728817 <br/>
 * @author 吴宇亮 on 2019/10/4 16:39
 */
@Controller
@RequestMapping("/framework/module/Jep.do")
public class JepController extends BaseControllerExt {

    /**
     * 公式和参数分开
     */
    @RequestMapping(params = "method=parse")
    @ResponseBody
    public ResponseVo parse(HttpServletRequest request) {
        try {
            String formula = request.getParameter("formula");
            String param = request.getParameter("param");
            if(StringUtils.isAnyBlank(formula)){
                return sendFail("参数错误");
            }

            Jep jep = new Jep();
            //默认只支持数学表达式，想要支持三元表达式，就要引入java表达式
            jep.setComponents(new StandardConfigurableParser(),new JavaOperatorTable());
            //添加参数
            if(StringUtils.isNoneBlank(param)){
                JSONObject jsonObject = JSON.parseObject(param);
                for (String key: jsonObject.keySet()) {
                    Object value = jsonObject.get(key);
                    jep.addVariable(key, value);
                }
            }
            //解析公式
            jep.parse(formula);
            //获取结果
            Object evaluate = jep.evaluate();
            return sendSuccess(evaluate);
        } catch (Exception e) {
            return sendError(e.getMessage());
        }
    }

    /**
     * 公式和参数整合在一起
     */
    @RequestMapping(params = "method=multiParse")
    @ResponseBody
    public ResponseVo multiParse(HttpServletRequest request) {
        try {
            String formula = request.getParameter("formula");
            if(StringUtils.isAnyBlank(formula)){
                return sendFail("参数错误");
            }

            Jep jep = new Jep();
            //参数跟公式在一起要用initMultiParse去解析
            jep.initMultiParse(formula);
            try {
                Node n;
                Object lastResult = null;
                while ((n = jep.continueParsing()) != null) {
                    Object res = jep.evaluate(n);
                    lastResult = res;
                }
                return sendSuccess(lastResult);
            }
            catch (JepException e) {
                return sendError(e.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return sendError(e.getMessage());
        }
    }



}
